const app = getApp()
Page({
  data: {
    disabled:false,
    ucode_show:false,
    message:'',
    ucode_user:{},
    item:{}
  },
  //监听页面初次载入完成
  onLoad: function (options) {
    app.getParam(options,(rel) => {
      this.data.id = rel.id
    });
  },
  //监听页面初次载入完成
  onShow: function () {
    app.loginState((rel)=>{
      this.getApi();
    },true)
  },
  //读取我的订单
  getApi: function() {
    app.api().Get('api/v1/ais/shoppay/index',{id: this.data.id},(result) => {
      if(result.code == 200){
        this.setData({
          item: result.data
        });
      }
    })
    if(!app.util().isNull(app.globalData.ucode)) {
      app.api().Get('openapi/v1/getCodeUser',{ucode:app.globalData.ucode},(rel) => {
        if (rel.code == 200) {
          this.setData({
            ucode_user: rel.data,
            ucode_show:true
          })
        }else{
          this.setData({
            ucode_show:false
          })
        }
      })
    }  
  },
  //提交订单
  onSubmit: function(){
    if (app.util().isNull(this.data.telphone)) {
      app.wxAlert('手机号码必须填写');
      return;
    }
    this.setData({disabled:true})
    setTimeout(()=>{
      this.setData({disabled:false})
    },3000)
    app.api().Post('api/v1/ais/shoppay/payment',{
      id:this.data.id,
      telphone:this.data.telphone,
      message:this.data.message,
      ucode: app.globalData.ucode
    },(res) => {
      if (res.code == 200) {
       app.doWechatPay(res.data,()=>{
          wx.redirectTo({
            url: 'user/index?active=2',
          })
       },()=>{
        wx.showModal({
          title:'微信支付',content:'你已取消支付,是否重试',
          cancelText:'取消',confirmText	:'重试',
          success:(res) => {
            if (res.confirm) {
              this.onSubmit();
            }
          }
        })
       })
      }
    })
  },
  //保存表单值
  telphone: function(e) {
    this.data.telphone = e.detail;
  },
  message: function(e) {
    this.data.message = e.detail;
  }
})